﻿using System;
using Echo.Context.UltiProNet.Contexts;
using Echo.Core.TestAbstraction.WebUITest.MbUnit;
using Echo.Models.UltiProNet;
using Echo.Models.UltimateSoftware.WebUITest.MbUnit;
using Echo.TestSupport.Database;
using MbUnit.Framework;
using Echo.Core.Common;

////*************************************************************************************************************************************
////Created by: Jannette Hamilton
////Date created: December 23, 2013
////Modified by: 
////Date modified: 
////Reason: CONVERTED FROM SWAT: SuiteCompliance.SuiteDotNet.SuiteWorkersCompensation.SuiteGuiTests.TestWorkercCompRiskRatesDetailA&B
////This test differs from the SWAT tests (which are very difficult to read). This tests verifies the GUI, adds US & CAN Risk Rates, and ensures that the original record is not changed.
////*************************************************************************************************************************************

namespace Echo.Automation.Compliance.BusinessRules.WorkerComp
{
    [TestFixture]
    [Author("Jannette Hamilton", "Jannette_Hamilton@ultimatesoftware.com")]
    [Category("Employee Pay: Deductions")]
    [Header("Config")]
    [Config(".NET")]
    //[Config("IEOnly")]
    //[Config("ChromeOnly")]
    //[Config("FFOnly")]

    public class WorkersCompRiskRates : MbUnitAutomatedTest
    {
        private UltiPro UltiPro;
        private UltiProDatabaseContext Database;

        [FixtureSetUp]
        public void FixtureSetUp()
        {
            UltiPro = Launch<UltiPro>();
            Database = Init<UltiProDatabaseContext>();
            Database.Company = ConfigDatabase("ULTIPRO_CALENDAR");
            Database.Company.AddRollBack("WcRisk");

            UltiPro.Pages.Login.LoginAs("AtnipJ", "password", Config.LanguageCode);
        }

        [FixtureTearDown]
        public void FixtureTearDown()
        {
            Database.Company.RollBackDatabase();
            UltiPro.Browser.Quit();
        }

        [SetUp]
        public void SetUp()
        {
            UltiPro.Macros.Navigate.ToBusinessRule("Workers' Compensation Risk Rates");
        }

        [TearDown]
        public void TearDown()
        {
        }

        #region Helper Methods
        public void SelectWorkersCompCode(string sWcCode, int rowNum)
        {
            UltiPro.Pages.BusinessRuleDetail.BusinessRuleDetailNonCodeTableGrid.Find(code => code.CodeFilter, FilterType.Is, sWcCode);
            UltiPro.Pages.BusinessRuleDetail.BusinessRuleDetailNonCodeTableGrid.Row(rowNum).Click(column => column.CodeLink);
        }

        public void SelectWorkersCompCodeByEffectiveDate(string fromDate)
        {
            UltiPro.Pages.BusinessRuleDetail.BusinessRuleDetailNonCodeTableGrid.Set(frmDate => frmDate.FromDate, fromDate);
            UltiPro.Pages.BusinessRuleDetail.BusinessRuleDetailNonCodeTableGrid.Click(filtBtn => filtBtn.FilterButton);
        }

        public void addNewRiskRate(string eeRate, string erRate, string modifier) 
        {
            var newEffDate = new DateTime(2013, 12, 29);

            UltiPro.Pages.WcRiskAddChg.NewEffectiveDate.Set(newEffDate);
            UltiPro.Pages.WcRiskAddChg.Set(txtbox => txtbox.EmployeeRateTextBox, eeRate);
            UltiPro.Pages.WcRiskAddChg.Set(txtbox => txtbox.EmployerRateTextBox, erRate);
            UltiPro.Pages.WcRiskAddChg.Set(txtbox => txtbox.ModifierTextBox, modifier);
            UltiPro.Pages.WcRiskAddChg.ButtonBar.Click(btn => btn.Save);
         }
        #endregion

        [Test(Order = 1)]
        public void verifyGuiFor4388WebPayrollCompany()
        {
            var newEffDate = DateTime.Today;

            SelectWorkersCompCode("4388", 0); // WEB PAYROLL COMPANY
            UltiPro.Pages.WcRiskAddChg.Like(header => header.PageHeaderLabel, "innerHTML", UltiPro.Localization.Strings.Add_slash_Change_Workers_apostrophe_Compensation_Risk_Rates);
            UltiPro.Pages.WcRiskAddChg.Is(page => page.StateValue, "innerHTML", "FL - Florida");
            UltiPro.Pages.WcRiskAddChg.Is(page => page.CodeValue, "innerHTML", "4388 - 4388");
            UltiPro.Pages.WcRiskAddChg.Is(page => page.CompanyValue, "innerHTML", "Web Payroll Company");
            UltiPro.Pages.WcRiskAddChg.Is(page => page.CurrentEffectiveDateValue, "innerHTML", "9/23/2010");
            UltiPro.Pages.WcRiskAddChg.Is(ddown => ddown.NewEffectiveDateValue, "value", newEffDate.ToString("MM/dd/yyyy"));
            UltiPro.Pages.WcRiskAddChg.Is(page => page.InsuranceCarrierDropDown, "innerHTML", "Workers Compensation - Insurance Carrier");

            UltiPro.Pages.WcRiskAddChg.Is(page => page.RatesSectionHeader, "innerHTML", UltiPro.Localization.Strings.Rates);
            UltiPro.Pages.WcRiskAddChg.Is(page => page.EmployeeRateTextBox, "value", "1,000.00 %");
            UltiPro.Pages.WcRiskAddChg.Is(page => page.EmployerRateTextBox, "value", "1,000.00 %");
            UltiPro.Pages.WcRiskAddChg.Is(page => page.ModifierTextBox, "value", "1.00");
            UltiPro.Pages.WcRiskAddChg.Is(page => page.TotalRateTextBox, "innerHTML", "2000.00");
        }

        [Test(Order = 2)]
        public void addNewRiskRateUs()
        {
            // NEW CODES ARE ADDED BY MODIFYING AN EXISTING CODE. THE ORIGINAL RECORD IS STILL RETAINED
            var newEffDate4388 = new DateTime(2013, 12, 29);

            // GIVEN THAT A US WORKERS COMP RISK RATE IS ADDED
            SelectWorkersCompCode("4388", 0); // 1st Code, WEB PAYROLL COMPANY
            addNewRiskRate(".1", ".2", ".5");
            
            // WHEN CHANGES ARE SAVED, THEN THE SUMMARY PAGE IS UPDATED
            SelectWorkersCompCodeByEffectiveDate("12/29/2013");
            UltiPro.Pages.BusinessRuleDetail.BusinessRuleDetailGrid.Row(col => col.Code, "4388", col => col.Description, "4388", col => col.TotalRate, "0.20 %").RowExists();
            UltiPro.Pages.BusinessRuleDetail.BusinessRuleDetailNonCodeTableGrid.Row(0).Click(code => code.CodeLink); // FIRST ROW

            // WHEN CHANGES ARE SAVED, THEN THE DETAIL PAGE IS UPDATED
            UltiPro.Pages.WcRiskAddChg.Like(header => header.PageHeaderLabel, "innerHTML", UltiPro.Localization.Strings.Add_slash_Change_Workers_apostrophe_Compensation_Risk_Rates);
            UltiPro.Pages.WcRiskAddChg.Is(page => page.StateValue, "innerHTML", "FL - Florida");
            UltiPro.Pages.WcRiskAddChg.Is(page => page.CodeValue, "innerHTML", "4388 - 4388");
            UltiPro.Pages.WcRiskAddChg.Is(page => page.CompanyValue, "innerHTML", "Web Payroll Company");
            UltiPro.Pages.WcRiskAddChg.Is(page => page.CurrentEffectiveDateValue, "innerHTML", "12/29/2013");
            UltiPro.Pages.WcRiskAddChg.Is(page => page.InsuranceCarrierDropDown, "innerHTML", "Workers Compensation - Insurance Carrier");
            UltiPro.Pages.WcRiskAddChg.Is(page => page.EmployeeRateTextBox, "value", "0.10 %");
            UltiPro.Pages.WcRiskAddChg.Is(page => page.EmployerRateTextBox, "value", "0.20 %");
            UltiPro.Pages.WcRiskAddChg.Is(page => page.ModifierTextBox, "value", "0.50");
            UltiPro.Pages.WcRiskAddChg.Is(page => page.TotalRateTextBox, "innerHTML", "0.20");
            UltiPro.Pages.WcRiskAddChg.ButtonBar.Click(btn => btn.Cancel);
            UltiPro.Alert.Accept();
        }

        [Test(Order = 3)]
        public void verifyOriginalRecordDidNotChangeUS()
        {
            var newEffDate = DateTime.Today;

            SelectWorkersCompCode("4388", 0); // 1st Code, WEB PAYROLL COMPANY
            UltiPro.Pages.WcRiskAddChg.Like(header => header.PageHeaderLabel, "innerHTML", UltiPro.Localization.Strings.Add_slash_Change_Workers_apostrophe_Compensation_Risk_Rates);
            UltiPro.Pages.WcRiskAddChg.Is(page => page.StateValue, "innerHTML", "FL - Florida");
            UltiPro.Pages.WcRiskAddChg.Is(page => page.CodeValue, "innerHTML", "4388 - 4388");
            UltiPro.Pages.WcRiskAddChg.Is(page => page.CompanyValue, "innerHTML", "Web Payroll Company");
            UltiPro.Pages.WcRiskAddChg.Is(page => page.CurrentEffectiveDateValue, "innerHTML", "9/23/2010");
            UltiPro.Pages.WcRiskAddChg.Is(ddown => ddown.NewEffectiveDateValue, "value", newEffDate.ToString("MM/dd/yyyy"));
            UltiPro.Pages.WcRiskAddChg.Is(page => page.InsuranceCarrierDropDown, "innerHTML", "Workers Compensation - Insurance Carrier");
            UltiPro.Pages.WcRiskAddChg.Is(page => page.EmployeeRateTextBox, "value", "1,000.00 %");
            UltiPro.Pages.WcRiskAddChg.Is(page => page.EmployerRateTextBox, "value", "1,000.00 %");
            UltiPro.Pages.WcRiskAddChg.Is(page => page.ModifierTextBox, "value", "1.00");
            UltiPro.Pages.WcRiskAddChg.Is(page => page.TotalRateTextBox, "innerHTML", "2000.00");
        }

        [Test(Order = 4)]
        public void addNewRiskRateCan()
        {
            // GIVEN THAT A CANADIAN WORKERS COMP RISK RATE IS ADDED
            SelectWorkersCompCode("CAB", 0); // 1st Code, First CANADIAN COMPANY
            addNewRiskRate("2.3", "6.7", "8.95");

            // WHEN CHANGES ARE SAVED, THEN THE SUMMARY PAGE IS UPDATED
            SelectWorkersCompCodeByEffectiveDate("12/29/2013"); 
            UltiPro.Pages.BusinessRuleDetail.BusinessRuleDetailGrid.Row(col => col.Code, "CAB", col => col.Description, "CAB", col => col.TotalRate, "62.27 %").RowExists();
            UltiPro.Pages.BusinessRuleDetail.BusinessRuleDetailNonCodeTableGrid.Row(1).Click(code => code.CodeLink); // 2nd row
            
            // WHEN CHANGES ARE SAVED, THEN THE DETAIL PAGE IS UPDATED
            UltiPro.Pages.WcRiskAddChg.Like(header => header.PageHeaderLabel, "innerHTML", UltiPro.Localization.Strings.Add_slash_Change_Workers_apostrophe_Compensation_Risk_Rates);
            UltiPro.Pages.WcRiskAddChg.Is(page => page.StateValue, "innerHTML", "AB - Alberta");
            UltiPro.Pages.WcRiskAddChg.Is(page => page.CodeValue, "innerHTML", "CAB - Alberta");
            UltiPro.Pages.WcRiskAddChg.Is(page => page.CompanyValue, "innerHTML", "The First Canadian Company");
            UltiPro.Pages.WcRiskAddChg.Is(page => page.CurrentEffectiveDateValue, "innerHTML", "12/29/2013");
            UltiPro.Pages.WcRiskAddChg.Is(page => page.InsuranceCarrierDropDown, "innerHTML", "");
            UltiPro.Pages.WcRiskAddChg.Is(page => page.EmployeeRateTextBox, "value", "2.30 %");
            UltiPro.Pages.WcRiskAddChg.Is(page => page.EmployerRateTextBox, "value", "6.70 %");
            UltiPro.Pages.WcRiskAddChg.Is(page => page.ModifierTextBox, "value", "8.95");
            UltiPro.Pages.WcRiskAddChg.Is(page => page.TotalRateTextBox, "innerHTML", "62.27");
            UltiPro.Pages.WcRiskAddChg.ButtonBar.Click(btn => btn.Cancel);
            UltiPro.Alert.Accept();
        }

        [Test(Order = 5)]
        public void verifyOriginalRecordDidNotChangeCan()
        {
            var newEffDate = DateTime.Today;

            SelectWorkersCompCode("CAB", 0); // 1st Code, First Canadian Company
            UltiPro.Pages.WcRiskAddChg.Like(header => header.PageHeaderLabel, "innerHTML", UltiPro.Localization.Strings.Add_slash_Change_Workers_apostrophe_Compensation_Risk_Rates);
            UltiPro.Pages.WcRiskAddChg.Is(page => page.StateValue, "innerHTML", "AB - Alberta");
            UltiPro.Pages.WcRiskAddChg.Is(page => page.CodeValue, "innerHTML", "CAB - Alberta");
            UltiPro.Pages.WcRiskAddChg.Is(page => page.CompanyValue, "innerHTML", "The First Canadian Company");
            UltiPro.Pages.WcRiskAddChg.Is(page => page.CurrentEffectiveDateValue, "innerHTML", "1/2/2006");
            UltiPro.Pages.WcRiskAddChg.Is(ddown => ddown.NewEffectiveDateValue, "value", newEffDate.ToString("MM/dd/yyyy"));
            UltiPro.Pages.WcRiskAddChg.Is(page => page.InsuranceCarrierDropDown, "innerHTML", "");
            UltiPro.Pages.WcRiskAddChg.Is(page => page.EmployeeRateTextBox, "value", "0.00 %");
            UltiPro.Pages.WcRiskAddChg.Is(page => page.EmployerRateTextBox, "value", "11.00 %");
            UltiPro.Pages.WcRiskAddChg.Is(page => page.ModifierTextBox, "value", "1.00");
            UltiPro.Pages.WcRiskAddChg.Is(page => page.TotalRateTextBox, "innerHTML", "11.00");
        }
    }
}